package com.iplant.ams.controller.ams;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.iplant.base.po.ServiceResult;
import com.iplant.base.utils.RetCode;
import com.iplant.base.utils.serializer.CloneTool;
import com.iplant.base.utils.general.StringUtils;
import com.iplant.basic.controller.BasicController;
import com.iplant.basic.service.po.bms.BMSEmployee;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.iplant.ams.service.po.ams.asset.AMSAssetClassification;

/**
 * @author PengYouWang
 * 2023-7-7 14:33:00
 */
@Slf4j
@RestController
@RequestMapping("/api/AMSAssetClassification")
public class AMSAssetClassificationController extends BasicController<AMSAssetClassification> {


    public AMSAssetClassificationController() {
        super(AMSAssetClassification.class);
    }

    /**
     * 批量删除
     */
    @PostMapping("/DeleteList")
    public Object DeleteList(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Map<String, Object> wResult;
        try {
            BMSEmployee wLoginUser = GetSession(request);

            if (!wParam.containsKey("data"))
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);

            List<AMSAssetClassification> wDataList = CloneTool.CloneArray(wParam.get("data"),
                    AMSAssetClassification.class);
            if (wDataList == null || wDataList.size() == 0 || wDataList.stream().anyMatch(p -> p.Active != 0)) {
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
            }

            if (wDataList.stream().anyMatch(p -> p.TypeEnum > 0)) {
                return GetResult(RetCode.SERVER_CODE_ERR, "固定资产不可删除!");
            }

            ServiceResult<Integer> wServiceResult = new ServiceResult<Integer>(0);
            for (AMSAssetClassification wAMSAssetClassification : wDataList) {

                wServiceResult = mPoService.DeletePO(wLoginUser, wAMSAssetClassification);

                if (!StringUtils.isEmpty(wServiceResult.getFaultCode()))
                    break;
            }

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServiceResult.Result);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            log.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }


    @PostMapping("/Delete")
    public Object Delete(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Object wResult = new Object();
        try {

            BMSEmployee wBMSEmployee = GetSession(request);

            if (!wParam.containsKey("data")) {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
                return wResult;
            }

            AMSAssetClassification wAMSAssetClassification = CloneTool.Clone(wParam.get("data"),
                    AMSAssetClassification.class);
            if (wAMSAssetClassification.TypeEnum > 0) {
                return GetResult(RetCode.SERVER_CODE_ERR, "固定资产不可删除!");
            }

            ServiceResult<Integer> wServiceResult = mPoService.DeletePO(wBMSEmployee, wAMSAssetClassification);

            if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "");
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
            }
        } catch (Exception ex) {
            log.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }
}
